package com.dattplace.util;

import java.util.Map;

/**
 * 保存用户相关的变量，供后续线程内方法使用
 * 
 * @author yaoyasong
 */
public class RequestContext {

	public static final String USER_ID = "user_id";
	public static final String USER_NAME = "user_name";
	public static final String USER_TYPE = "user_type";

	private static final ThreadLocal<Map<String, Object>> tl = new ThreadLocal<Map<String, Object>>();

	public static Map<String, Object> getAttributes() {
		Map<String, Object> attributes = tl.get();
		return attributes;
	}

	public static void setAttributes(Map<String, Object> attributes) {
		tl.set(attributes);
	}

	public static String getUserId() {
		Map<String, Object> attributes = RequestContext.getAttributes();
		if (attributes != null && attributes.containsKey(USER_ID)) {
			return (String) attributes.get(USER_ID);
		}
		return "";
	}

	public static String getUserName() {
		Map<String, Object> attributes = RequestContext.getAttributes();
		if (attributes != null && attributes.containsKey(USER_NAME)) {
			return (String) attributes.get(USER_NAME);
		}
		return "";
	}

	public static String getUserType() {
		Map<String, Object> attributes = RequestContext.getAttributes();
		if (attributes != null && attributes.containsKey(USER_TYPE)) {
			return (String) attributes.get(USER_TYPE);
		}
		return "";
	}

	public static void remove(){
	    tl.remove();
    }
}
